package com.example.pro1.service.lian.oldService;


import com.example.pro1.mapper.lian.oldMapper.customerMapperOld;
import com.example.pro1.entity.lian.oldPojo.customerOld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class customeService {
    @Autowired
    customerMapperOld customerMapperOld;

    /*获取客户数量*/
    public Integer getCustomerNums(){
        return customerMapperOld.getCustomerNums();
    }

    /*查询客户信息*/
    public List<customerOld> getCustomersInfo(Map<String,Object> map){
        if (map.containsKey("k") && map.containsKey("num")){
            map.put("k",Integer.parseInt((String) map.get("k")));
            map.put("num",Integer.parseInt((String) map.get("num")));
            map.put("kNum",(Integer)map.get("k")*(Integer)map.get("num"));
        }
        return customerMapperOld.getCustomersInfo(map);
    }
    /*添加客户信息*/
    public boolean addOneCustomer(Map<String,Object> map){
        /*首先根据code*/
        return customerMapperOld.insertOneCustomer(map);
    }

    /*修改客户信息*/
    public boolean updateCustomerInfo(Map<String,Object> map){
        return  customerMapperOld.updateCustomerByInfo(map);
    }

    /*根据Id修改客户信息*/
    public boolean updateCustomerById(Map<String,Object> map){
        /*首先需要检查cid是否存在*/
        if (map.containsKey("cId")&&map.size()>1){
            /*将cId转换为整形*/
            map.put("cId",Integer.parseInt((String)map.get("cId")));
            return customerMapperOld.updateCustomerById(map);
        }else {
            return false;
        }

    }

    /*根据ID删除客户信息*/
    public boolean deleteOneCustomerById(Map<String,Object> map){
        return customerMapperOld.deleteCustomerByInfoById(map);
    }


//    /*根据code返回openId*/
//    public String getOpenIdByCode(String code){
//        // 1.接收小程序发送的code
//        // 2.开发者服务器 登录凭证校验接口 appi + appsecret + code
//        JSONObject SessionKeyOpenId = WechatUtil.getSessionKeyOrOpenId(code);
//        // 3.接收微信接口服务 获取返回的参数
//        String openid = SessionKeyOpenId.getString("openid");
//        return openid;
//    }

    /*判断是否有customer对象*/
    /*如果存在cWXOpenId,就应该返回cid*/
    /*如果不存在cWXOpenId,应该返回-1*/
    public Long haveCustomer(Map<String,Object> map){
        /*首先检查cWXOpenId是否存在*/
        if (map.containsKey("cWXOpenId")){
            /*首先根据各种信息进行查询*/
            List<customerOld> customersInfo = customerMapperOld.getCustomersInfo(map);
            if (customersInfo.size()>0){
                return customersInfo.get(0).getCId();
            }else {
                return  Long.parseLong(""+-1);
            }
        }else {
            return Long.parseLong(""+-1);
        }
    }


    /*微信小程序添加客户信息*/
    public Integer addNewCustomer(Map<String,Object> map){
        /*首先检查是否有相关的信息*/
        if (map.containsKey("cWXOpenId")&&
                map.containsKey("cName")&&
                map.containsKey("cPhone")&&
                map.containsKey("cImageSrc")&&
                map.containsKey("cWXNumber")
        ){
            map.put("cId",-1);
            if (customerMapperOld.insertOneCustomer(map)){
                /*如果插入成功，返回对应的cId*/
                return (Integer) map.get("cId");
            }else {
                return -1;
            }

        }else {
            return -1;
        }
    }

}
